/*
 * cali_panel.c
 *
 *  Created on: 2023年2月23日
 *      Author: Lw
 */
#include "fpga_reg.h"
#include "cali_panel.h"
#include "dri_hal_can.h"
#include "tele_data_transfer.h"
#include "camera_process.h"
#include "ff.h"
#include "pl_comm.h"
#include "xil_cache.h"

FRESULT CaliDataDDR2EMMC(u8 port, u8 type);
FRESULT CaliDataEMMC2DDR(u8 port, u8 type);
FRESULT CaliDataReplace2Inuse(u8 port, u8 type);
FRESULT CaliDataDefault2Inuse(u8 port, u8 type);
FRESULT CaliDataDDR2EMMCDefault(u8 port, u8 type);
FRESULT CaliDataReplace2Default(u8 port, u8 type);
FRESULT CaliDataEMMCReplace2DDR(u8 port, u8 type);

static FIL fil;     /* File object */
extern S_tEquipmentStatus g_tEquipmentStatus;      /* 设备的状态信息结构体 */

FRESULT caliFileInit(void)
{
    u8 initData[EVERYWRLEN*4] = {0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	u8 uniqueValue = 0x00;
    FRESULT rc;
    UINT br;
    UINT bw;
    /* 相机1平场在使用数据文件 */
    rc = f_open(&fil, EMMC_CAM1_INUSE_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_INUSE_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机1平场待替换数据文件 */
    rc = f_open(&fil, EMMC_CAM1_REPLASE_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_REPLASE_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机1平场默认数据文件 */
    rc = f_open(&fil, EMMC_CAM1_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_DEFAULT_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);

    /* 相机1暗场在使用数据文件 */
    rc = f_open(&fil, EMMC_CAM1_INUSE_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_INUSE_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机1暗场待替换数据文件 */
    rc = f_open(&fil, EMMC_CAM1_REPLASE_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_REPLASE_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机1暗场默认数据文件 */
    rc = f_open(&fil, EMMC_CAM1_DEFAULT_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_DEFAULT_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机1LED数据文件 */
    rc = f_open(&fil, EMMC_CAM1_LED_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM1_LED_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
        uniqueValue = 0x40;
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);


    /* 相机2平场在使用数据文件 */
    rc = f_open(&fil, EMMC_CAM2_INUSE_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_INUSE_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
        uniqueValue = 0x00;
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机2平场待替换数据文件 */
    rc = f_open(&fil, EMMC_CAM2_REPLASE_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_REPLASE_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机2平场默认数据文件 */
    rc = f_open(&fil, EMMC_CAM2_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_DEFAULT_FLAT_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);

    /* 相机2暗场在使用数据文件 */
    rc = f_open(&fil, EMMC_CAM2_INUSE_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_INUSE_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机2暗场待替换数据文件 */
    rc = f_open(&fil, EMMC_CAM2_REPLASE_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_REPLASE_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机2暗场默认数据文件 */
    rc = f_open(&fil, EMMC_CAM2_DEFAULT_DARK_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_DEFAULT_DARK_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);
    /* 相机2LED数据文件 */
    rc = f_open(&fil, EMMC_CAM2_LED_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM2_LED_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
        uniqueValue = 0x40;
		for(u32 dIndex = 0; dIndex < cycSize; dIndex++)
		{
			initData[dIndex] = uniqueValue;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			rc = f_write(&fil, initData, cycSize, &bw);
		}
    }
    f_close(&fil);

    /* 相机参数数据文件 */
    rc = f_open(&fil, EMMC_CAM_PARAM_FILE, FA_OPEN_EXISTING);
    if(rc != FR_OK){
        rc = f_open(&fil, EMMC_CAM_PARAM_FILE, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
		f_lseek(&fil, 0);
		rc = f_write(&fil, &g_tEquipmentStatus.caliUseStatus, 1, &bw);
    }
    /* 从emmc中读出数据  */
    rc = f_open(&fil, EMMC_CAM_PARAM_FILE, FA_READ);
    rc = f_read(&fil, &g_tEquipmentStatus.caliUseStatus, 1, &br);
    if(rc != FR_OK){
        f_close(&fil);
    }
    f_close(&fil);

    return rc;
}

void caliParamSave(void)
{
    UINT bw;
	f_open(&fil, EMMC_CAM_PARAM_FILE, FA_OPEN_EXISTING | FA_WRITE);
	f_lseek(&fil, 0);
	f_write(&fil, &g_tEquipmentStatus.caliUseStatus, 1, &bw);
	f_close(&fil);
}

u8 caliDataInit(void)
{

	caliFileInit();

	u8 state = 0;

	if(CaliDataEMMC2DDR(CAMERA_ONE,DARK) != FR_OK){
		state = 1;
	}
	if(CaliDataEMMC2DDR(CAMERA_ONE,FLAT)!= FR_OK){
		state|=(1<<1);
	}
	/*
	if(CaliDataEMMC2DDR(CAMERA_TWO,DARK)!= FR_OK){
		state|=(1<<2);
	}
	if(CaliDataEMMC2DDR(CAMERA_TWO,FLAT)!= FR_OK){
		state|=(1<<3);
	}
	*/
	CaliDataEMMC2DDR(CAMERA_ONE,LED);
//	CaliDataEMMC2DDR(CAMERA_TWO,LED);
	s_Cam1CaliData.DarkFlag = CALIINIT;
	s_Cam2CaliData.DarkFlag = CALIINIT;
	s_Cam1CaliData.FlatFlag = CALIINIT;
	s_Cam2CaliData.FlatFlag = CALIINIT;
	s_Cam1CaliData.DarkCaliState = 0;
	s_Cam1CaliData.FlatCaliState = 0;
	s_Cam2CaliData.DarkCaliState = 0;
	s_Cam2CaliData.FlatCaliState = 0;
	s_Cam1CaliData.FlatRload = 0;
	return state;
}

void caliDataProcess(void)
{
	if(s_Cam1CaliData.DarkFlag == REPLASE){
		if(CaliDataReplace2Inuse(CAMERA_ONE,DARK) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus | 0x01);
			caliParamSave();
		}
		s_Cam1CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam1CaliData.DarkFlag == DEFAULT){
		if(CaliDataDefault2Inuse(CAMERA_ONE,DARK) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus & 0x0E);
			caliParamSave();
		}
		s_Cam1CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam1CaliData.DarkFlag == CREAT){
		CaliDataReplace2Default(CAMERA_ONE,DARK);
		s_Cam1CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam1CaliData.FlatFlag == REPLASE){
		if(CaliDataReplace2Inuse(CAMERA_ONE,FLAT) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus | 0x02);
			caliParamSave();
		}
		s_Cam1CaliData.FlatFlag = CALIINIT;
	}
	else if(s_Cam1CaliData.FlatFlag == DEFAULT){
		if(CaliDataDefault2Inuse(CAMERA_ONE,FLAT) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus & 0x0D);
			caliParamSave();
		}
		s_Cam1CaliData.FlatFlag = CALIINIT;
	}
	else if(s_Cam1CaliData.FlatFlag == CREAT){
		CaliDataReplace2Default(CAMERA_ONE,FLAT);
		s_Cam1CaliData.FlatFlag = CALIINIT;
	}

	if(s_Cam2CaliData.DarkFlag == REPLASE){
		if(CaliDataReplace2Inuse(CAMERA_TWO,DARK) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus | 0x04);
			caliParamSave();
		}
		s_Cam2CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam2CaliData.DarkFlag == DEFAULT){
		if(CaliDataDefault2Inuse(CAMERA_TWO,DARK) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus & 0x0B);
			caliParamSave();
		}
		s_Cam2CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam2CaliData.DarkFlag == CREAT){
		CaliDataReplace2Default(CAMERA_TWO,DARK);
		s_Cam2CaliData.DarkFlag = CALIINIT;
	}
	else if(s_Cam2CaliData.FlatFlag == REPLASE){
		if(CaliDataReplace2Inuse(CAMERA_TWO,FLAT) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus | 0x08);
			caliParamSave();
		}
		s_Cam2CaliData.FlatFlag = CALIINIT;
	}
	else if(s_Cam2CaliData.FlatFlag == DEFAULT){
		if(CaliDataDefault2Inuse(CAMERA_TWO,FLAT) == FR_OK)
		{
			g_tEquipmentStatus.caliUseStatus = (g_tEquipmentStatus.caliUseStatus & 0x07);
			caliParamSave();
		}
		s_Cam2CaliData.FlatFlag = CALIINIT;
	}
	else if(s_Cam2CaliData.FlatFlag == CREAT){
		CaliDataReplace2Default(CAMERA_TWO,FLAT);
		s_Cam2CaliData.FlatFlag = CALIINIT;
	}

	if(g_tEquipmentStatus.ucCamera_1_WorkMode == CALI_DARK){
		if(ReadCaliState(CAMERA_ONE, DARK) && s_Cam1CaliData.DarkCaliState == 0){
			s_Cam1CaliData.DarkCaliState = 1;
			CaliDataDDR2EMMC(CAMERA_ONE,DARK);
		}
	}

	if(g_tEquipmentStatus.ucCamera_1_WorkMode == CALI_FLAT){
		if(s_Cam1CaliData.FlatRload == 1){
			s_Cam1CaliData.FlatRload = 0;
			CaliDataEMMCReplace2DDR(CAMERA_ONE,DARK);
			CaliDataEMMCReplace2DDR(CAMERA_ONE,LED);
			CaliDataEMMCReplace2DDR(CAMERA_TWO,DARK);
			CaliDataEMMCReplace2DDR(CAMERA_TWO,LED);
		}
		if(ReadCaliState(CAMERA_ONE, FLAT) && s_Cam1CaliData.FlatCaliState == 0){
			s_Cam1CaliData.FlatCaliState = 1;
			CaliDataDDR2EMMC(CAMERA_ONE,FLAT);
		}
	}

	if(g_tEquipmentStatus.ucCamera_2_WorkMode == CALI_DARK){
		if(ReadCaliState(CAMERA_TWO, DARK) && s_Cam2CaliData.DarkCaliState == 0){
			s_Cam2CaliData.DarkCaliState = 1;
			CaliDataDDR2EMMC(CAMERA_TWO,DARK);
		}
	}

	if(g_tEquipmentStatus.ucCamera_2_WorkMode == CALI_FLAT){
		if(ReadCaliState(CAMERA_TWO, DARK) && s_Cam1CaliData.DarkCaliState == 0){
			s_Cam2CaliData.FlatCaliState = 1;
			CaliDataDDR2EMMC(CAMERA_TWO,FLAT);

		}
	}

}

void caliChooseData(u8 port,u8 uiValue)
{
	if(port == CAMERA_ONE)
	{
		if(uiValue == 0){
			s_Cam1CaliData.DarkFlag = REPLASE;
		}
		else if(uiValue == 1){
			s_Cam1CaliData.FlatFlag = REPLASE;
		}
		else if(uiValue == 2){
			s_Cam1CaliData.DarkFlag = DEFAULT;
		}
		else if(uiValue == 3){
			s_Cam1CaliData.FlatFlag = DEFAULT;
		}
		else if(uiValue == 4){
			s_Cam1CaliData.DarkFlag = CREAT;
		}
		else if(uiValue == 5){
			s_Cam1CaliData.FlatFlag = CREAT;
		}

	}
	else if(port == CAMERA_TWO)
	{
		if(uiValue == 0){
			s_Cam2CaliData.DarkFlag = REPLASE;
		}
		else if(uiValue == 1){
			s_Cam2CaliData.FlatFlag = REPLASE;
		}
		else if(uiValue == 2){
			s_Cam2CaliData.DarkFlag = DEFAULT;
		}
		else if(uiValue == 3){
			s_Cam2CaliData.FlatFlag = DEFAULT;
		}
		else if(uiValue == 4){
			s_Cam2CaliData.DarkFlag = CREAT;
		}
		else if(uiValue == 5){
			s_Cam2CaliData.FlatFlag = CREAT;
		}
	}
}
/*
 * DDR中的标定数据写入到EMMC待替换的数据文件中
 * port=1:相机1;port=2:相机2
 * type=1:暗场数据;type=2:平场数据
 * */
FRESULT CaliDataDDR2EMMC(u8 port, u8 type)
{
    UINT bw;
    FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);

	if(port == CAMERA_ONE && type == DARK)
	{
		res = f_open(&fil, EMMC_CAM1_REPLASE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM1_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM1_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		res = f_open(&fil, EMMC_CAM1_REPLASE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM1_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM1_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		res = f_open(&fil, EMMC_CAM2_REPLASE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM2_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM2_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		res = f_open(&fil, EMMC_CAM2_REPLASE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM2_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM2_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	return res;
}

/*
 * 生成默认的标定数据文件
 * port=1:相机1;port=2:相机2
 * type=1:暗场数据;type=2:平场数据
 * */
FRESULT CaliDataDDR2EMMCDefault(u8 port, u8 type)
{
    UINT bw;
    FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);

	if(port == CAMERA_ONE && type == DARK)
	{
		res = f_open(&fil, EMMC_CAM1_DEFAULT_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM1_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM1_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		res = f_open(&fil, EMMC_CAM1_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM1_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM1_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		res = f_open(&fil, EMMC_CAM2_DEFAULT_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM2_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM2_PL2PS_DARK_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		res = f_open(&fil, EMMC_CAM2_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++)
		{
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++)
			{
				Xil_DCacheFlushRange(CAM2_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4, 4);
				dataBuff[dIndex] = Xil_In32(CAM2_PL2PS_FLAT_BASEADDR+index*cycSize+dIndex*4);
			}
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 向emmc中写入数据  */
			res = f_write(&fil, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
		}
		f_close(&fil);
	}
	return res;
}

/*
 * EMMC中的在使用的标定数据加载到DDR中
 * port=1:相机1;port=2:相机2
 * type=1:暗场数据;type=2:平场数据;3:LED数据
 * */
FRESULT CaliDataEMMC2DDR(u8 port, u8 type)
{
   /* f_read 返回的已读取的字节数  */
	UINT br;
	FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	if(port == CAMERA_ONE && type == DARK)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_INUSE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_INUSE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == LED)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_LED_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_LED_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_LED_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_INUSE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_INUSE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == LED)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_LED_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_LED_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_LED_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	return res;
}

/*
 * EMMC中的在新标定数据加载到DDR中
 * port=1:相机1;port=2:相机2
 * type=1:暗场数据;type=2:平场数据;3:LED数据
 * */
FRESULT CaliDataEMMCReplace2DDR(u8 port, u8 type)
{
   /* f_read 返回的已读取的字节数  */
	UINT br;
	FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	if(port == CAMERA_ONE && type == DARK)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_ONE && type == LED)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM1_LED_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM1_LED_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM1_LED_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_PS2PL_DARK_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_PS2PL_FLAT_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	else if(port == CAMERA_TWO && type == LED)
	{
		/* 打开一个只读的文件  */
		res = f_open(&fil, EMMC_CAM2_LED_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&fil, (index*cycSize));
			/* 从emmc中读出数据  */
			res = f_read(&fil, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&fil);
				return res;
			}
			for(u32 dIndex = 0; dIndex < EVERYWRLEN; dIndex++){
				Xil_Out32((CAM2_LED_BASEADDR+index*cycSize+dIndex*4),dataBuff[dIndex]);
				Xil_DCacheFlushRange((CAM2_LED_BASEADDR+index*cycSize+dIndex*4), 4);
			}
		}
		/* 关闭文件  */
		f_close(&fil);
	}
	return res;
}



/*EMMC中待替换的标定数据覆盖到在使用的标定数据*/
FRESULT CaliDataReplace2Inuse(u8 port, u8 type)
{
	FIL filRep;     /* File object */
	FIL filInuse;   /* File object */
	UINT br;
	UINT bw;
	FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	if(port == CAMERA_ONE && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM1_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_INUSE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM1_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_INUSE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM2_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_INUSE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM2_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_INUSE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	return res;
}

/*EMMC中待替换的标定数据覆盖到默认的标定数据*/
FRESULT CaliDataReplace2Default(u8 port, u8 type)
{
	FIL filRep;     /* File object */
	FIL filInuse;   /* File object */
	UINT br;
	UINT bw;
	FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	if(port == CAMERA_ONE && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM1_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_DEFAULT_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM1_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM2_REPLASE_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_DEFAULT_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM2_REPLASE_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_DEFAULT_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	return res;
}
/*EMMC中默认的标定数据覆盖到在使用的标定数据*/
FRESULT CaliDataDefault2Inuse(u8 port, u8 type)
{
	FIL filRep;     /* File object */
	FIL filInuse;   /* File object */
	UINT br;
	UINT bw;
	FRESULT res;
	u32 dataBuff[EVERYWRLEN]={0};
	u32 cycNum = (CAMCALIDATALEN/(EVERYWRLEN*4));
	u16 cycSize = (EVERYWRLEN*4);
	if(port == CAMERA_ONE && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM1_DEFAULT_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_INUSE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_ONE && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM1_DEFAULT_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM1_INUSE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == DARK)
	{
		res = f_open(&filRep, EMMC_CAM2_DEFAULT_DARK_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_INUSE_DARK_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	else if(port == CAMERA_TWO && type == FLAT)
	{
		res = f_open(&filRep, EMMC_CAM2_DEFAULT_FLAT_FILE, FA_READ);
		if(res != FR_OK){
			return res;
		}
		res = f_open(&filInuse, EMMC_CAM2_INUSE_FLAT_FILE, FA_OPEN_EXISTING | FA_WRITE);
		if(res != FR_OK){
			return res;
		}
		for(u32 index = 0; index < cycNum; index ++){
			/*移动文件指针*/
			f_lseek(&filRep, (index*cycSize));
			f_lseek(&filInuse, (index*cycSize));
			res = f_read(&filRep, dataBuff, cycSize, &br);
			if(res != FR_OK){
				f_close(&filRep);
				return res;
			}
			res = f_write(&filInuse, dataBuff, cycSize, &bw);
			if(res != FR_OK){
				f_close(&filInuse);
				return res;
			}
		}
		f_close(&filRep);
		f_close(&filInuse);
	}
	return res;
}
/*************************************************************************
**************************************************************************
* 全局变量：     无
* 输入变量：
*               u32 uiSwitch,           LED开关，0：关， 1: 开
*               u32 uiPwm,              占空比
*               u32 uiFreq              PWM周期数
*************************************************************************/
void LedControl(u32 uiLedNum, u32 uiSwitch, u32 uiPwm, u32 uiFreq)
{
    if(uiLedNum == 1){
        /* 1.配置占空比  */
        Xil_Out32(LED1_PWM, uiPwm);
        Xil_DCacheFlushRange(LED1_PWM, 4);
        /* 2.配置PWM周期数  */
        Xil_Out32(LED1_PWM_CYCLE, uiFreq);
        Xil_DCacheFlushRange(LED1_PWM_CYCLE, 4);
        /* 3.使能控制设置  */
        Xil_Out32(LED1_ENABLE, ENABLE);
        Xil_DCacheFlushRange(LED1_ENABLE, 4);
        /* 4.配置LED开关  */
        Xil_Out32(LED1_SWITCH, uiSwitch);
        Xil_DCacheFlushRange(LED1_SWITCH, 4);
    }
}

